package cn.org.donald.framework.util;

import cn.org.donald.framework.pojo.BaseModel;
import org.springframework.beans.BeanUtils;

import java.lang.reflect.Field;

/**
 * @author : Donald
 * @since : 2020/11/10 14:03
 */
public class BeanUtil extends BeanUtils {

    /**
     *  判断对象是否为空或者属性值只有null和""
     * @param t
     * @param <T>
     * @return
     */
    public static <T extends BaseModel> boolean  isEmptyOrClassFieldAllEmpty(T t){
        if (t == null){
            return true;
        }
        try {
            Class<? extends BaseModel> clazz = t.getClass();
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                String name = declaredField.getName();
                if ("serialVersionUID".equals(name)){
                    declaredField.setAccessible(true);
                    if (declaredField.get(t) != null || "".equals(declaredField.get(t))){
                        return false;
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return true;
    }
}
